var g_targetProxy = null;
var TargetProxy = cc.Node.extend({
    _chooseFlag : null,
    _chooseTarget : null,
    /////////////////////////////////
    ctor : function(){
        this._super();
        g_targetProxy=this;
        g_targetProxy.retain();
    },
    onEnter:function () {
        this._super();
        this.addChooseFlag();
    },
    onExit : function(){
        this._super();
    },
    setTarget : function(target){
        g_chooser = target;
        this._chooseTarget = target;
        cc.log("target choose: "+target);
    },
    isSameTarget : function(target){
        if( target==this._chooseTarget ){
            cc.log("****same target ");
            return true;
        }else{
            cc.log("****not same target ");
            return false;
        }
    },
    isSameTarget : function(target){
        return g_chooser==target?true:false;
    },
    addChooseFlag:function(){
        var filename=MainResource("target.png");
        this._chooseFlag = new cc.Sprite( filename );
        this._chooseFlag.setVisible(false);
        this.addChild(this._chooseFlag);
    },
    update : function(dt){
        if(g_chooser){
            var pos = g_chooser.getPosition()+ this._chooseFlag.getPosition();
            this._chooseFlag.setPosition( pos  );
        }
    },
    showChooseTarget : function(show,target){
        if(show){
            if( target ){
                if(!this.isSameTarget(target)){
                    this.setTarget(target);
                    this._chooseFlag.stopAllActions();
                    this._chooseFlag.setPosition( cc.pAdd( target.getPosition(),cc.p(0,50) )  );
                    this._chooseFlag.setVisible(true);
                    this._chooseFlag.runAction( new cc.RepeatForever(
                        new cc.Sequence(
                            new cc.ScaleTo(0.2,0.84),
                            new cc.ScaleTo(0.15,1)
                        )
                    ) );
                    cc.log("show target flag");
                }else{
                    this.setTarget(null);
                    this._chooseFlag.stopAllActions();
                    this._chooseFlag.setVisible(false);
                    cc.log("hide target flag");
                }
            }else{
                cc.log("target is invalid :msg from target flag");
            }
        }else{
            this.setTarget(null);
            this._chooseFlag.stopAllActions();
            this._chooseFlag.setVisible(false);
            cc.log("hide target flag");
        }
    }
});
IsSameTarget=function(target){
    if(g_targetProxy){
        return g_targetProxy.isSameTarget(target);
    }else{
        cc.log("g_targetProxy is invalid");
        return false;
    }
}
ShowChooseFlag=function(show,target){
    if(g_targetProxy){
        g_targetProxy.showChooseTarget(show,target);
    }else{
        cc.log("g_targetProxy is invalid");
    }
}